package org.xmppbot.net.nio;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolDecoder;
import org.apache.mina.filter.codec.ProtocolDecoderOutput;

import java.nio.charset.Charset;

/**
 * Decodes readed from input stream bytes to string.
 *
 * @author Stanislav Panov
 * @author Bulat Nigmatullin
 */
class ProtocolDecoderImpl implements ProtocolDecoder {

    private static final Charset INPUTSTREAM_CHARSET = Charset.forName("UTF-8");

    @Override
    public void decode(IoSession arg0, IoBuffer ioBuffer, ProtocolDecoderOutput protocolDecoderOutput)
            throws Exception {
        protocolDecoderOutput.write(new String(ioBuffer.array(), INPUTSTREAM_CHARSET).trim());
    }

    @Override
    public void dispose(IoSession arg0) throws Exception {
        // omit it
    }

    @Override
    public void finishDecode(IoSession arg0, ProtocolDecoderOutput arg1)
            throws Exception {
        // omit it
    }

}
